package cevent.source.cloudcenter.system.controller.admin;

import cevent.source.cloudcenter.server.dto.RolesDto;
import cevent.source.cloudcenter.server.dto.PageDto;
import cevent.source.cloudcenter.server.dto.ResponseDataDto;
import cevent.source.cloudcenter.server.service.RolesService;
import cevent.source.cloudcenter.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author cevent
 * @description 角色管理表控制层
 */
@RestController
@RequestMapping("/admin/roles")
public class RolesController {
    private static final Logger LOG = LoggerFactory.getLogger(RolesController.class);
    //AOP日志环绕
    public static final String BUSINESS_NAME="角色管理表";
    @Resource
    private RolesService rolesService;

    //1.dto-roles获取
    @RequestMapping("/dtoList")
    public List<RolesDto> getDtoList() {
        return rolesService.rolesDtoList();
    }


    /*2.前端分页设置，后端pageInfo处理
    前端传参方式一：表单方式--》formData 前端：直接传对象   后端参数：(Dto对象)
    前端传参方式二：流的方式--》data{param1:value1,param2:vlaue2}  后端参数：(@RequestBody Dto dto对象)
     */
    @PostMapping("/pageList")
    public ResponseDataDto list(@RequestBody PageDto pageDto) {
        LOG.info("查看controller的pageDto：{}", pageDto);
        ResponseDataDto responseData = new ResponseDataDto();
        rolesService.getRolesPageDtoList(pageDto);
        responseData.setResponseData(pageDto);
        return responseData;
    }

    //3.新增，流方式传参，加入@RequestBody
    @PostMapping("/save")
    public ResponseDataDto save(@RequestBody RolesDto rolesDto) {
        LOG.info("传入的roles DTO：{}", rolesDto);

        ValidatorUtil.requiredEmpty(rolesDto.getName(), "角色名称");
        ValidatorUtil.requiredLength(rolesDto.getName(), "角色名称", 3, 50);
        ValidatorUtil.requiredLength(rolesDto.getPrivilegeDesc(), "权限描述", 3, 255);
        ResponseDataDto responseData = new ResponseDataDto();
        rolesService.save(rolesDto);
        responseData.setResponseData(rolesDto);
        return responseData;
    }

    /**
     * 指定请求的格式为Delete
     * 4.删除模块，如果为多个参数，就定义多个/{param}/{param}
     */
    @DeleteMapping("/del/{uniId}")
    public ResponseDataDto del(@PathVariable String uniId) {
        LOG.info("传入的roles uniId：{}", uniId);
        ResponseDataDto responseData = new ResponseDataDto();
        rolesService.delete(uniId);
        return responseData;
    }

    /**
     * 保存资源
     */
    @PostMapping("/save/rolesResources")
    public ResponseDataDto saveRolesResources(@RequestBody RolesDto rolesDto){
        ResponseDataDto responseData=new ResponseDataDto();
        ValidatorUtil.requiredEmpty(rolesDto.getName(),"角色名称");
        RolesDto roles=rolesService.saveRolesResources(rolesDto);
        responseData.setResponseData(roles);
        return responseData;
    }

    /**
     * 保存员工
     */
    @PostMapping("/save/rolesEmployee")
    public ResponseDataDto saveRolesEmployee(@RequestBody RolesDto rolesDto){
        ResponseDataDto responseData=new ResponseDataDto();
        RolesDto roles=rolesService.saveRolesEmployeeByIds(rolesDto);
        responseData.setResponseData(roles);
        return responseData;
    }
}
